package com.boot.springboot.component.qlexpress.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.boot.springboot.component.qlexpress.domain.KeyContent;
import com.boot.springboot.component.qlexpress.domain.LogContentReq;
import com.boot.springboot.component.qlexpress.domain.LogData;
import com.boot.springboot.component.qlexpress.domain.WifiSsid;
import com.boot.springboot.component.qlexpress.enums.LogNameEnum;
import com.boot.springboot.component.qlexpress.service.DecryptHelper;
import com.boot.springboot.component.qlexpress.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author luowenjie
 * @since 2022/2/13 17:43
 */

@RestController
@Slf4j
@Api(tags = "收集接口")
@RequestMapping("/collect/log")
public class LogController {

    @Autowired
    LogService logService;

    @ApiOperation(value = "用于生活助手日志上传接口", notes = "上传扫描设备，局域网设备信息，音量改变，服务开关日志等")
    @PostMapping(value = "/v1")
    public Object getV1Log(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestBody List<KeyContent> keyContents) {
        Collection<LogData> logs = new ArrayList<>();
        Collection<WifiSsid> wifis = new ArrayList<>();
        if (keyContents.size() > 0) {
            for (int i = 0; i < keyContents.size(); i++) {
                String decrypt = "";
                LogContentReq logContent;
                String content = "";
                String key = "";
                try {
                    content = keyContents.get(i).getContent();
                    key = keyContents.get(i).getKey();
                    decrypt = DecryptHelper.decrypt(content, key);
                    logContent = JSON.parseObject(decrypt, LogContentReq.class);
                } catch (Exception e) {
                    log.error("日志解码错误:key---{}\n,content:{}", key, content);
                    continue;
                }
                LogData logEntiy = new LogData();
                BeanUtils.copyProperties(logContent, logEntiy);
                logEntiy.setCreateTime(LocalDateTime.now());
                logEntiy.setAndroidVer(logContent.getAndroidVer());
                logEntiy.setAppVer(logContent.getAppVer());
                logEntiy.setOsBootTime(logContent.getOsBootTime());
                logEntiy.setSdkVer(logContent.getSdkVer());

                if (Objects.isNull(logContent.getCore())) {
                    logEntiy.setCore("");
                }
                if (Objects.isNull(logContent.getCOpenId())) {
                    logEntiy.setCOpenId("");
                }
                logs.add(logEntiy);
                Map<String, Object> data = logContent.getData();
                Object o = data.get("list");
                Map dataMap = new HashMap();
                if (logContent.getName().equals(LogNameEnum.WIFI.getName())) {
                    JSONArray o1 = (JSONArray) o;
                    for (int j = 0; j < o1.size(); j++) {
                        dataMap = (Map)o1.get(j);
                        dataMap.put("ActiveId",logContent.getActiveId());
                        dataMap.put("LogId",logContent.getLogId());
                        break;
                    }
                }else if (logContent.getName().equals(LogNameEnum.VOLUMECHANGE.getName())) {
                    dataMap.put("Active_Id",logContent.getActiveId());
                    dataMap.put("Log_Id",logContent.getLogId());
                    dataMap.put("old_volume_value",Integer.valueOf(logContent.getData().get("old").toString()));
                    dataMap.put("new_volume_value",Integer.valueOf(logContent.getData().get("new").toString()));
                }
                List valueList = new ArrayList(dataMap.values());

                List keyList = new ArrayList(dataMap.keySet());
                Map saveMap = new HashMap();
                saveMap.put("tablename","device_volume");
                saveMap.put("keys",keyList);
                saveMap.put("params",dataMap);
                logService.saveLog(saveMap);
            }
        }
        return "提交日志成功";
    }
}
